/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.utilities;

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.Window;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.StringTokenizer;
import javax.jnlp.ServiceManager;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

/**
 *
 * @author Administrator
 */
public class Utilities {
    private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();

    private Utilities(){
    }

    public static boolean runningFromWebStart(){
        return ServiceManager.getServiceNames()!=null;
    }
    public static String getURLFileName(URL url){
        String path= url.getPath();
        return path.substring(path.lastIndexOf("/")+1);
    }

    private static BufferedImage createCompatibleImage(int width, int height){
        return GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height);
    }
    public static BufferedImage createTranslucentImage(int width, int height) {

        return GraphicsEnvironment.getLocalGraphicsEnvironment().
                    getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

    }

    public static BufferedImage createGradientImage(int width, int height, Color gradient1, Color gradient2){
        BufferedImage gradientImage= createCompatibleImage(width, height);
        GradientPaint gradientPaint= new GradientPaint(0, 0, gradient1, 0, height, gradient2, false);
        Graphics2D g2=(Graphics2D)gradientImage.getGraphics();
        g2.setPaint(gradient2);
        g2.fillRect(0, 0, width, height);
        g2.dispose();
        return gradientImage;
    }
    public static BufferedImage createCradientMask(int width, int height, int orientation){
        BufferedImage gradientImage= new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2= (Graphics2D)gradientImage.createGraphics();
        GradientPaint gradientPaint= new GradientPaint(0.0f,0.0f,
                new Color(1.0f,1.0f,1.0f,1.0f),
                orientation == SwingConstants.HORIZONTAL? width : 0.0f,
                orientation == SwingConstants.VERTICAL? height : 0.0f,
                new Color(1.0f, 1.0f, 1.0f, 0.0f));
        g2.setPaint(gradientPaint);
        g2.fill(new Rectangle2D.Double(0, 0, width, height));
        g2.dispose();
        gradientImage.flush();
        return gradientImage;
    }

    public static Color deriveColorAlpha(Color base, int alpha) {
        return new Color(base.getRed(), base.getGreen(), base.getBlue(), alpha);
    }

    public static Color deriveColorHSB(Color base, float dH, float dS, float dB) {
        float hsb[] = Color.RGBtoHSB(
                base.getRed(), base.getGreen(), base.getBlue(), null);

        hsb[0] += dH;
        hsb[1] += dS;
        hsb[2] += dB;
        return Color.getHSBColor(
                hsb[0] < 0? 0 : (hsb[0] > 1? 1 : hsb[0]),
                hsb[1] < 0? 0 : (hsb[1] > 1? 1 : hsb[1]),
                hsb[2] < 0? 0 : (hsb[2] > 1? 1 : hsb[2]));

    }

    public static String getHTMLColorString(Color color) {
        String red = Integer.toHexString(color.getRed());
        String green = Integer.toHexString(color.getGreen());
        String blue = Integer.toHexString(color.getBlue());

        return "#" +
                (red.length() == 1? "0" + red : red) +
                (green.length() == 1? "0" + green : green) +
                (blue.length() == 1? "0" + blue : blue);
    }

    public static void printColor(String key, Color color) {
       float hsb[] = Color.RGBtoHSB(
                color.getRed(), color.getGreen(),
                color.getBlue(), null);
       System.out.println(key+": RGB=" +
               color.getRed() + ","+ color.getGreen() + ","+ color.getBlue() + "  " +
                "HSB=" + String.format("%.0f%n",hsb[0]*360) + "," +
                            String.format("%.3f%n",hsb[1]) + "," +
                            String.format("%.3f%n", hsb[2]));
    }

    public static void setToplevelLocation(Window toplevel, Component component,
            int relativePosition) {

        Rectangle compBounds = component.getBounds();

        // Convert component location to screen coordinates
        Point p = new Point();
        SwingUtilities.convertPointToScreen(p, component);

        int x;
        int y;

        // Set frame location to be centered on panel
        switch (relativePosition) {
            case SwingConstants.NORTH: {
                x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
                y = p.y - toplevel.getHeight();
                break;
            }
            case SwingConstants.EAST: {
                x = p.x + compBounds.width;
                y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
                break;
            }
            case SwingConstants.SOUTH: {
                x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
                y = p.y + compBounds.height;
                break;
            }
            case SwingConstants.WEST: {
                x = p.x - toplevel.getWidth();
                y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
                break;
            }
            case SwingConstants.NORTH_EAST: {
                x = p.x + compBounds.width;
                y = p.y - toplevel.getHeight();
                break;
            }
            case SwingConstants.NORTH_WEST: {
                x = p.x - toplevel.getWidth();
                y = p.y - toplevel.getHeight();
                break;
            }
            case SwingConstants.SOUTH_EAST: {
                x = p.x + compBounds.width;
                y = p.y + compBounds.height;
                break;
            }
            case SwingConstants.SOUTH_WEST: {
                x = p.x - toplevel.getWidth();
                y = p.y + compBounds.height;
                break;
            }
            default:
            case SwingConstants.CENTER: {
                x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
                y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
            }
        }
        toplevel.setLocation(x, y);
    }

    public static String byteArrayToHexString(byte[] buf){
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i)
        {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }

    public static String byteArrayToFormatHexString(byte[] buf){
        char[] chars = new char[2 * buf.length + buf.length];
        for (int i = 0; i < buf.length; ++i)
        {
            chars[3 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[3 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
            chars[3 * i + 2] = 32;
        }
        return new String(chars);
    }

    public static String[] resolveEmailAddress(String s){
        StringTokenizer token = new StringTokenizer(s, ";");
        String[] address = new String[token.countTokens()];
        int i=0;
        while (token.hasMoreTokens()) {
            address[i] = token.nextToken();
        }
        return address;
    }

}
